package com.piece.admin.thymeleaf;

import com.piece.admin.model.TbSysDict;
import com.piece.admin.service.TbSysDictService;
import com.piece.core.framework.support.dto.ConfigDTO;
import com.piece.core.message.enums.SmsChannelType;
import com.piece.file.enums.FileStorageType;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * thymeleaf 实现字典读取
 */
@Service("dict")
public class DictService {

    @Resource
    private TbSysDictService tbSysDictService;

    /**
     * 根据字典类型查询字典数据信息
     *
     * @param dictGroup 字典组名
     */
    public List<TbSysDict> getDataGroup(String dictGroup) {
        return tbSysDictService.findByDicGroup(dictGroup);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictGroup 字典组名
     * @param dictValue 字典键值
     */
    public String getDataLabel(String dictGroup, String dictValue) {
        TbSysDict dic = tbSysDictService.findByGroupValue(dictGroup, dictValue);
        return null == dic ? "" : dic.getDictLabel();
    }

    /**
     * 获取系统客户端类别
     */
    public List<ConfigDTO> getClientType(String type) {
        switch (type) {
            case "FileStorageType":
                return Arrays.stream(FileStorageType.values())
                        .map(storage -> ConfigDTO.builder().name(storage.getName()).value(storage.getName()).build()).collect(Collectors.toList());
            case "SmsChannelType":
                return Arrays.stream(SmsChannelType.values())
                        .map(channel -> ConfigDTO.builder().name(channel.getName()).value(channel.getCode()).build()).collect(Collectors.toList());
        }

        return Collections.EMPTY_LIST;
    }

    /**
     * 获取授权方式
     */
    public List<ConfigDTO> getAuthorizedGrantTypes() {
        List<ConfigDTO> list = new ArrayList<>();
        list.add(ConfigDTO.builder().name("username_code").value("username_code").build());
        list.add(ConfigDTO.builder().name("mobile_code").value("mobile_code").build());
        list.add(ConfigDTO.builder().name("open_id").value("open_id").build());
        list.add(ConfigDTO.builder().name("authorization_code").value("authorization_code").build());
        list.add(ConfigDTO.builder().name("refresh_token").value("refresh_token").build());
        list.add(ConfigDTO.builder().name("client_credentials").value("client_credentials").build());
        list.add(ConfigDTO.builder().name("implicit").value("implicit").build());
        return list;
    }
}
